package action;

import Impl.FriendDaoImpl;
import entity.Friend;
import entity.User;
import sun.security.x509.URIName;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "FriendOpt")
public class FriendOpt extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("----------正在执行FriendOpt--------");
        request.setCharacterEncoding("utf-8");
        String method = request.getParameter("method");
        System.out.println(method);
        HttpSession session = request.getSession();
        String uname = (String) session.getAttribute("uname");
        int fid =Integer.parseInt(request.getParameter("id"));
//        System.out.println(fid);
        FriendDaoImpl friendDao = new FriendDaoImpl();
//        如果是删除，删除好友信息
        if (method!=null && method.equals("del")){
            System.out.println("-----正在删除好友信息-------");
            friendDao.delete(fid,uname);
            request.getRequestDispatcher("/friend.jhtml").forward(request,response);
        }
        System.out.println("---------正在编辑好友信息----------");
        Friend friend = friendDao.findFriendById(uname,fid);
        session.setAttribute("friend",friend);
        request.getRequestDispatcher("/editFriend.jsp").forward(request,response);


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
